'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 读取配置的操作类
 * @author sundbcn@gmail.com (sun debing)
 */

var pomelo = require('pomelo');
var fs = require('fs');
var moment = require('moment');

exports.second_per_day = 86400;

exports.load = function (config_name) {
    var env = 'development';
    if (!!pomelo.app) {
        env = pomelo.app.get('env');
    }
    var config_path = __dirname + '/../../config/' + config_name + '.json';
    var config_list;
    if (fs.existsSync(config_path)) {
        config_list = require(config_path);
    } else {
        config_list = require('../../config/' + env + '/' + config_name + '.json');
    }
    if (!config_list) {
        throw new Error('config is empty');
    }
    return config_list;
};

// 取开服时间
exports.getOpenGameTime = function () {
    return this.load('open').open_time;
};

// 取开服日期
exports.getOpenGameDate = function () {
    var open_time = this.getOpenGameTime();
    return moment.unix(open_time).startOf('day').unix();
};

// 取当前开服后第几天
exports.getDayAfterOpenGame = function () {
    var open_date = this.getOpenGameDate();
    var now_date = moment().startOf('day').unix();
    return Math.ceil((now_date + 1 - open_date) / this.second_per_day);
};
